package org.example.setup.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.example.model.domain.feeback.ExpressFeeback;
import org.example.model.domain.feeback.FeedbackSupplement;
import org.example.model.dto.feeback.ExpressFeebackDto;
import org.example.setup.mapper.FeebackMapper;
import org.example.setup.service.FeebackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class FeebackServiceImpl implements FeebackService {

    @Autowired
    private FeebackMapper feebackMapper;

    @Override
    public int add(ExpressFeebackDto expressFeeback) {
        try {
            // 创建一个新的 ExpressFeeback 对象，并将 ExpressFeebackDto 对象的属性复制到新对象中
            ExpressFeeback expressFeeback1 = new ExpressFeeback();
            BeanUtil.copyProperties(expressFeeback, expressFeeback1);
            // 设置反馈时间和处理时间为当前时间
            expressFeeback1.setFeebackTime(LocalDateTime.now());
            expressFeeback1.setProcessingTime(LocalDateTime.now());
            // 将反馈添加到数据库中
            return feebackMapper.add(expressFeeback1);
        } catch (Exception e) {
            // 记录日志或进行适当的错误处理
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public ExpressFeebackDto selectByRegisterIp(String registerIp) {
        try {
            // 根据注册 IP 从数据库中查询反馈
            ExpressFeeback expressFeeback = feebackMapper.selectByRegisterIp(registerIp);
            ExpressFeebackDto expressFeebackDto = new ExpressFeebackDto();
            // 将查询到的反馈对象的属性复制到新的 ExpressFeebackDto 对象中
            BeanUtil.copyProperties(expressFeeback, expressFeebackDto);
            // 如果反馈对象的 imgs 属性不为空，将其分割成字符串列表，并设置到 ExpressFeebackDto 对象的 imgsList 属性中
            if (expressFeeback.getImgs()!= null) {
                String[] split = expressFeeback.getImgs().split(",");
                List<String> imgsList = new ArrayList<>();
                for (String s : split) {
                    imgsList.add(s);
                }
                expressFeebackDto.setImgsList(imgsList);
            }
            return expressFeebackDto;
        } catch (Exception e) {
            // 记录日志或进行适当的错误处理
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int supplementAdd(FeedbackSupplement supplement) {
        try {
            // 添加反馈补充
            return feebackMapper.supplementAdd(supplement);
        } catch (Exception e) {
            // 记录日志或进行适当的错误处理
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public FeedbackSupplement findSupplement(Long fid) {
        try {
            // 查找反馈补充
            return feebackMapper.findSupplement(fid);
        } catch (Exception e) {
            // 记录日志或进行适当的错误处理
            e.printStackTrace();
            return null;
        }
    }
}
